package using.association;

import using.chushi_frame;

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.net.URL;
import java.sql.*;


public class association_login extends JFrame
{
    public static String str;
    public static String username;
    public association_login()
    {
        setTitle("Student association management system-association");
        setBounds(200, 200, 800, 600);
        Container container = getContentPane();

        JPanel panel1 = new JPanel();
        panel1.setBorder(BorderFactory.createTitledBorder("login interface"));
        panel1.setLayout(null);

        JTextField jt1 = new JTextField();
        JPasswordField jp = new JPasswordField();

        JButton jb1 = new JButton();// 确认按钮
        JButton jb2 = new JButton();// 取消按钮

        JLabel jl1 = new JLabel("use name:");
        JLabel jl2 = new JLabel("password:");

        jl1.setForeground(Color.blue);
        jl2.setForeground(Color.blue);

        panel1.add(jb1);
        panel1.add(jb2);
        panel1.add(jl1);
        panel1.add(jl2);
        panel1.add(jp);
        panel1.add(jt1);

        jb1.setText("login");
        jb2.setText("cancel");

        JLabel jk1 = new JLabel();
        URL url = chushi_frame.class.getResource("use2.png");
        Icon icon = new ImageIcon(url);
        jk1.setIcon(icon);
        jk1.setHorizontalAlignment(SwingConstants.CENTER);
        jk1.setOpaque(true);
        jk1.setBounds(50,20, 700, 300);
        panel1.add(jk1);

        jp.setBounds(300, 400, 200, 40);
        jt1.setBounds(300, 300, 200, 40);
        jb1.setBounds(200, 500, 100, 40);
        jb2.setBounds(400, 500, 100, 40);
        jl1.setBounds(150, 300, 80, 40);
        jl2.setBounds(150, 400, 80, 40);

        jp.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e) {
                SQL_sever.connection();
                String str4 = jt1.getText();
                username = str4;
                String str1="select * from [use]";
                try
                {
                    SQL_sever.sql=SQL_sever.con.createStatement();
                    SQL_sever.res=SQL_sever.sql.executeQuery(str1);
                    while(SQL_sever.res.next())
                    {
                        String str2 = SQL_sever.res.getString(1);
                        String str3 = SQL_sever.res.getString(2);
                        if (new String(jp.getPassword()).equals(str3) && username.equals(str2)) {
                            System.out.println("Login successful");
                            JOptionPane.showMessageDialog(null, "Login successful");
                            association_frame.tan();
                            setVisible(false);
                        }
                        else {
                            JOptionPane.showMessageDialog(null, "Incorrect account name or password");
                        }
                    }
                }
                catch (SQLException e1)
                {
                    e1.printStackTrace();
                }
            }
        });

        jb1.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                SQL_sever.connection();
                String str5 = jt1.getText();
                String str1;
                username = jt1.getText();
                if (str5.equals("members"))
                {
                    str1 = "select * from[use] where use_id='members'";
                }
                else
                {
                    str1 = "select * from[use]where use_id='a'";
                }
                try
                {
                    SQL_sever.sql = SQL_sever.con.createStatement();
                    SQL_sever.res = SQL_sever.sql.executeQuery(str1);
                    String str2 = null, str3 = null;
                    while (SQL_sever.res.next()) {
                        str2 = SQL_sever.res.getString(1);
                        str3 = SQL_sever.res.getString(2);
                    }
                    if (new String(jp.getPassword()).equals(str3) && jt1.getText().equals(str2)
                            && ("members".equals(jt1.getText()))) {
                        System.out.println("Login successful");
                        JOptionPane.showMessageDialog(null, "Login successful");
                        association_frame.tan();
                        setVisible(false);
                    }
                    else if (new String(jp.getPassword()).equals(str3) && jt1.getText().equals(str2) && "a".equals(jt1.getText()))
                    {
                        JOptionPane.showMessageDialog(null, "Login successful");
                        association_frame.tan();
                        setVisible(false);
                    } else {
                        JOptionPane.showMessageDialog(null, "Incorrect account name or password");
                    }
                }
                catch (SQLException e1)
                {
                    e1.printStackTrace();
                }
            }
        });

        jb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });

        JLabel jl3 = new JLabel();
        JLabel jl4 = new JLabel();

        jl3.setBounds(0, 0, this.getWidth(), this.getHeight());
        jl4.setBounds(0, 0, this.getWidth(), this.getHeight());

        container.addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                int width = e.getComponent().getWidth();
                int heigth = e.getComponent().getHeight();

                jl3.setBounds(0, 0, width, heigth);
                jl4.setBounds(0, 0, width, heigth);
            }
        });
        jl3.setHorizontalAlignment(SwingConstants.CENTER);
        jl3.setOpaque(true);
        jl4.setOpaque(false);

        panel1.add(jl4);

        container.add(panel1);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void tan() {
        // TODO Auto-generated method stub
        association_login login2 = new association_login();
        login2.setVisible(true);
    }
}
